package com.atguigu.gulimall.member.config;

import feign.RequestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Classname GuliFeignConfig
 * @Description TODO
 * @Date 2021/10/14 14:27
 * @Author Anonymous
 */
@Configuration
public class GuliFeignConfig {

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor() {
        return template -> {
            //使用RequestContextHolder拿到刚进来的请求
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if (requestAttributes != null) {
                HttpServletRequest request = requestAttributes.getRequest();
                //将老请求得到cookie信息放到feign请求上
                String cookie = request.getHeader("Cookie");
                template.header("Cookie", cookie);
                //同步请求头数据，主要是cookie
            }
        };
    }
}
